package com.douziit.meilianoa.base.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Auther: lh <br>
 * Date: 2018/12/10 15:30 <br>
 * Description:  URL(编码、解码、参数解析)<br>
 */
public class URLEncodeUtil {
    private final static String ENCODE = "GBK";
    /**
     * URL 解码
     *
     * @return String
     * @author lifq
     * @date 2015-3-17 下午04:09:51
     */
    public static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * URL 转码
     *
     * @return String
     * @author lifq
     * @date 2015-3-17 下午04:10:28
     */
    public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 正则替换
     * @param url
     * @param name
     * @param value
     * @return
     */
    public static String replaceUrlParameterReg(String url, String name, String value) {
        if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(value)) {
            url = url.replaceAll("(" + name + "=[^&]*)", name + "=" + value);
        }
        return url;
    }
    /**
     * 正则替换
     * @param url
     * @param name
     * @param value
     * @return
     */
    public static String getUrlParameterReg(String url, String name) {

        Map<String, String> mapRequest = new HashMap<String, String>();

        String[] arrSplit=null;

        String strUrlParam=TruncateUrlPage(url);
        if(strUrlParam==null)
        {
            return "";
        }
        //每个键值为一组 www.2cto.com
        arrSplit=strUrlParam.split("[&]");
        for(String strSplit:arrSplit)
        {
            String[] arrSplitEqual=null;
            arrSplitEqual= strSplit.split("[=]");

            //解析出键值
            if(arrSplitEqual.length>1)
            {
                //正确解析
                mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);

            }
            else
            {
                if(arrSplitEqual[0]!="")
                {
                    //只有参数没有值，不加入
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        return mapRequest.get(name);
    }
    /**
     * 去掉url中的路径，留下请求参数部分
     * @param strURL url地址
     * @return url请求参数部分
     */
    private static String TruncateUrlPage(String strURL)
    {
        String strAllParam=null;
        String[] arrSplit=null;

        strURL=strURL.trim().toLowerCase();

        arrSplit=strURL.split("[?]");
        if(strURL.length()>1)
        {
            if(arrSplit.length>1)
            {
                if(arrSplit[1]!=null)
                {
                    strAllParam=arrSplit[1];
                }
            }
        }

        return strAllParam;
    }
    public static void main(String[] args) {
        String str ="/activePage?wxto=0C29975119DE037B4F25CD119CF4CF11&activity2018topid=oz9Zj07Jg5O9IfAyVauT-GL-CSfM&activity2018topunid=o-NUA0l8fxm2_5fmK_ocpyxrP1dE&subscribe=0&timestamp=1516008942920&-NUA0l8fxm2_5fmK_ocpyxrP1dE&oaccesstoken=5_xJYY3gnUTD4pqJFc7_ltbvclb0ZRKOFBEc5BXAwi7vjZndRAN58AkPazqUJDGzBbrw-J1ag1PF0C-S39SqJflA&m=1&wxt=C9A58DA3804E46981BF4B78C8C862743&-NUA0oHjJjCcyGe-wJdEFYjwrfU&-NUA0oHjJjCcyGe-wJdEFYjwrfU&-NUA0oHjJjCcyGe-wJdEFYjwrfU";
        System.out.println( getUrlParameterReg(str,"wxto"));
    }

}
